package com.daotingli.backendprograms.Service.Admin.AuthorBook;

import com.daotingli.backendprograms.Repository.Admin.AuthorBook.AuthorBookRepository;
import com.daotingli.backendprograms.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class AuthorBookService {
    @Autowired
    private AuthorBookRepository authorBookRepository;

    public Result getAllAuthorBooks() {

        return Result.success(authorBookRepository.getAllAuthorBooks());
    }

    public Result getAllAuthors() {
        return Result.success(authorBookRepository.getAllAuthors());
    }

    public Result addAuthorBook(String bookname, String authorId, String bookState) {
        Map<String, Object> result = authorBookRepository.addAuthorBook(bookname, authorId, bookState);
        if("success".equals(result.get("status"))){
            return Result.success();
        }else{
            return Result.error((String) result.get("message"));
        }
    }

    public Result deleteAuthorBook(Integer id) {
        Map<String, Object> result = authorBookRepository.deleteAuthorBook(id);
        if("success".equals(result.get("status"))){
            return Result.success();
        }else{
            return Result.error((String) result.get("message"));
        }
    }

    public Result updateAuthorBook(Integer id, String bookname, String authorId, String bookState) {
        Map<String, Object> result = authorBookRepository.updateAuthorBook(id, bookname, authorId, bookState);

        if("success".equals(result.get("status"))){
            return Result.success();
        }else{
            return Result.error((String) result.get("message"));
        }
    }

    public Result getAllBooks() {
        return Result.success(authorBookRepository.getAllBooks());
    }

    public Result getAllNoDeleteBooks() {
        return Result.success(authorBookRepository.getAllNoDeleteBooks());
    }
}
